package br.com.vagacerta.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class PagamentoDAO {
	
	private Connection connection;
	
	@Autowired
	public PagamentoDAO(DataSource dataSource) {
		try {
			this.connection = dataSource.getConnection();

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	
	public boolean verificaStatusPagamentoCupomPeloNomeCupom(long nomeCupom){
		try {
			String sql = "SELECT IF((SELECT STATUS_PAGAMENTO FROM PAGAMENTO WHERE COD_CUPOM ="
					+ " (SELECT COD_CUPOM FROM CUPOM WHERE NOME_CUPOM = ?)) = 1,'true','false') AS 'STATUS_PAGAMENTO_CUPOM'";

			PreparedStatement stmt;
			stmt = connection.prepareStatement(sql);
			
			stmt.setLong(1, nomeCupom);

			ResultSet rs = stmt.executeQuery();
			boolean statusPagamentoCupom = false;

			while (rs.next()) {
				statusPagamentoCupom = Boolean.valueOf(rs.getString("STATUS_PAGAMENTO_CUPOM"));
			}

			rs.close();
			stmt.close();

			return statusPagamentoCupom;

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}


	public boolean existePagamentoPorCodigoCupom(Long codigoCupom) {
		try {
			String sql = "SELECT IF((SELECT STATUS_PAGAMENTO FROM PAGAMENTO WHERE COD_CUPOM = ?) = 1,'true','false') AS 'STATUS_PAGAMENTO_CUPOM'";

			PreparedStatement stmt;
			stmt = connection.prepareStatement(sql);
			
			stmt.setLong(1, codigoCupom);

			ResultSet rs = stmt.executeQuery();
			boolean statusPagamentoCupom = false;

			while (rs.next()) {
				statusPagamentoCupom = Boolean.valueOf(rs.getString("STATUS_PAGAMENTO_CUPOM"));
			}

			rs.close();
			stmt.close();

			return statusPagamentoCupom;

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	
	public boolean existePagamentoAtivoPorCodigoCupom(Long codigoCupom) {
		try {
			String sql = "SELECT IF((SELECT STATUS_PAGAMENTO FROM PAGAMENTO WHERE COD_CUPOM = ? AND STATUS_PAGAMENTO = 1) = 1,'true','false') AS 'STATUS_PAGAMENTO_CUPOM'";

			PreparedStatement stmt;
			stmt = connection.prepareStatement(sql);
			
			stmt.setLong(1, codigoCupom);

			ResultSet rs = stmt.executeQuery();
			boolean statusPagamentoCupom = false;

			while (rs.next()) {
				statusPagamentoCupom = Boolean.valueOf(rs.getString("STATUS_PAGAMENTO_CUPOM"));
			}

			rs.close();
			stmt.close();

			return statusPagamentoCupom;

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}


	public void efetuaPagamentoPeloNomeCupom(long codigoCupom) throws SQLException {
		
		String sql = "INSERT INTO PAGAMENTO (COD_CUPOM, DATA_HORA, STATUS_PAGAMENTO, COD_FUNCIONARIO)"
					+ " VALUES (?, SYSDATE(), 1, (SELECT COD_FUNCIONARIO FROM FUNCIONARIO WHERE NOME_FUNCIONARIO = 'ADMINISTRADOR'))";

		PreparedStatement stmt;
		stmt = connection.prepareStatement(sql);
		
		stmt.setLong(1, codigoCupom);

		stmt.execute();
		stmt.close();

	}
	
}
